#!/usr/bin/env python
# -*- coding: utf-8 -*- 

#Kiwi Bot
#Make by @nge1nCorsic4 in 2010
#Modified by @nge1nCorsic4 in 2011


#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or
#(at your option) any later version.

#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.

#You should have received a copy of the GNU General Public License along
#with this program; if not, write to the Free Software Foundation, Inc.,
#51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

import kiwi, kiwi.plugin, kiwi.events, kiwi.lang,  kiwi.config

class tk(kiwi.plugin.Plugin):
	def onStart(self):
		self.dic = {}
		self.lang = kiwi.lang.Lang(self,'tk')
		ServNumber = kiwi.config.configuration().kiwi.kiwidir
		self.config = kiwi.config.configuration('@kiwi/conf/'+ServNumber+'/tk.cfg')
		self.level = int(self.config.settings.level)
		self.register = self.main.getPlugin('admin').mod.registerCommand
		self.registerEvent(kiwi.events.EVT_CLIENT_KILL, self.teamkill)
		self.registerEvent(kiwi.events.EVT_CLIENT_DISCONNECT, self.client_deco)
		self.register('fa', int(self.config.levels.fa), self._fa, "000600")
		self.register('fp', int(self.config.levels.fp), self._fp, "000601")
	def onStop(self):
		self.main.getPlugin('admin').mod.deleteCommand('fa')
		self.main.getPlugin('admin').mod.deleteCommand('fp')
	def red(self,killer,dead):
		red,blue,spec = self.clients.getTeams()
		nb = 0
		for client in red:
			if client.id == killer:
				nb = nb+1
			elif client.id == dead:
				nb = nb+1
		if nb == 2:
			return True
		return False
	def blue(self,killer,dead):
		red,blue,spec = self.clients.getTeams()
		nb = 0
		for client in blue:
			if client.id == killer:
				nb = nb+1
			elif client.id == dead:
				nb = nb+1
		if nb == 2:
			return True
		return False
	def teamkill(self, event):
		dead, killer, vars = event.dead, event.killer, event.vars
		if dead != killer :
			if killer != 1022:
				noRespond = (	'MOD_FALLING',
								'MOD_SLIME',
								'MOD_CRUSH',
								'MOD_TARGET_LASER',
								'MOD_WATER',
								'MOD_LAVA',
								'MOD_TRIGGER_HURT',
								'UT_MOD_FLAG')
				if not vars[-1] in noRespond :
					C_killer = self.clients.getClientById(killer)
					if C_killer:
						if C_killer[0].rights < self.level:
							if self.red(killer,dead) or self.blue(killer,dead):
								C_dead = self.clients.getClientById(dead)[0]
								self.output.tell(dead,self.lang.lang(C_dead,"00601")%C_killer[0].name)
								dic = self.dic
								if dic.has_key(dead):
									list = dic[dead]
									list.append(killer)
									dic[dead] = list
									nb = 0
									for a in dic:
										for b in dic[a] :
											if killer == b:
												nb += 1
									if nb == 3:
										self.output.tell(killer,self.lang.lang(C_killer[0],"00602"))
									elif nb > 3:
										self.output.send('kick %s'%killer)
										self.output.say('^1%s ^4%s.'%(C_killer[0].name,self.lang.lang(C_killer[0],"00603")))
								else:
									dic[dead]=[killer]
	def client_deco(self, event):
		dic = self.dic
		for d in dic:
			list =  dic[d]
			while event.id in list:
				list.remove(event.id)
			dic[d] = list
		if dic.has_key(event.id):
			del dic[event.id]
	def _fa(self, event):
		dic, dead = self.dic, event.id
		if dic.has_key(dead):
			if len(dic[dead]) >= 2 :
				dic[dead] = []
				self.output.tell(dead,self.lang.lang(event,"00606"))
	def _fp(self, event):
		dic, dead = self.dic, event.id
		client = self.main.clients.getClientById
		if dic.has_key(dead):
			if len(dic[dead]) >= 2 :
				id = dic[dead][-1]
				dic[dead] = dic[dead][:-1]
				self.output.tell(id,'%s ^4%s'%(client(dead)[0].name,self.lang.lang(client(id)[0],"00604")))
				self.output.tell(dead,self.lang.lang(event,"00605")%client(id)[0].name)